/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2015年4月9日         Root.Lu
 */

package com.coocaa.app.gamecenter.framework;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.coocaa.x.framework.app.CoocaaApplication;
import com.coocaa.x.service.litecache.data.client.LiteImageCache;
import com.coocaa.x.service.litecache.data.client.LiteImageCache.LiteImageCacheLoadListener;

public class AppIconCache {
    private static final String CACHE_FILE = "app_icon_cache";
    private static SharedPreferences sharedPreferences = null;

    public static synchronized void addAppIcon(String pkgName, String url) {
        if (sharedPreferences == null) {
            Context context = CoocaaApplication.getContext();
            sharedPreferences = context.getSharedPreferences(CACHE_FILE,
                    Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE);
        }
        Editor editor = sharedPreferences.edit();
        editor.putString(pkgName, url);
        editor.commit();
    }

    public static synchronized boolean getAppIcon(String pkgName, int width, int height,
                                                  LiteImageCacheLoadListener listener) {
        if (sharedPreferences == null) {
            Context context = CoocaaApplication.getContext();
            sharedPreferences = context.getSharedPreferences(CACHE_FILE,
                    Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE);
        }
        String url = sharedPreferences.getString(pkgName, "");
        if (!url.equals("")) {
            LiteImageCache.getInstance().load(url, listener, width, height);
            return true;
        }
        return false;
    }
}
